<?php
class ManageQuestionsController extends ExtendedController
{
    function __construct()
    {
        parent::__construct();
        if(Session::getInstance()->get('permission') != '2')
        {
            $_SESSION['ref'] = substr($_SERVER['PATH_INFO'],1);
            Utility::error('Bạn không đủ quyền hạn để sử dụng chức năng này', 'index');
            exit();
        }

    }

    public function index($subjectID=null, $sectionID=null, $questionID=null, $optionID=null )
    {
        //tao model
        $manageQuestionsModel = $this->loadModel('managequestions');
        
        $lectureID = Session::getInstance()->get('id');
        if ($lectureID == null);
                //Khong co tham so nao truyen vao
        else
        {
            if ($subjectID == null)
            {
                //*** xay dung view subject ***
                //tham so truyen vao: $lectureID

                $subjects = $manageQuestionsModel->getSubjectList($lectureID);

                //tao view
                $subjectListView = $this->loadView('subjects');

                //truyen du lieu vao
                $subjectListView->appendBlockData(array('lectureID'=>$lectureID));
                $subjectListView->appendBlockData(array('subjects'=>$subjects));

                //ve
                $subjectListView->render();

            }
            else
            {
                if ($sectionID == null)
                {
                    //*** xay dung view section ***
                    //tham so truyen vao: $lectureID, $subjectID

                    $sections = $manageQuestionsModel->getSectionList($subjectID);

                    //tao view
                    $sectionListView = $this->loadView('sections');

                    //truyen du lieu vao
                    $sectionListView->appendBlockData(array('lectureID'=>$lectureID));
                    $sectionListView->appendBlockData(array('subjectID'=>$subjectID));
                    $sectionListView->appendBlockData(array('sections'=>$sections));

                    //ve
                    $sectionListView->render();
                }
                else
                {
                    if($questionID == null)
                    {
                        //*** xay dung view question ***
                        //tham so truyen vao: $lectureID, $subjectID, $sectionID

                        $questions = $manageQuestionsModel->getQuestionList($sectionID);

                        //tao view
                        $questionListView = $this->loadView('questions');

                        //truyen du lieu vao
                        $questionListView->appendBlockData(array('lectureID'=>$lectureID));
                        $questionListView->appendBlockData(array('subjectID'=>$subjectID));
                        $questionListView->appendBlockData(array('sectionID'=>$sectionID));
                        $questionListView->appendBlockData(array('questions'=>$questions));

                        //ve
                        $questionListView->addJs('managequestions');
                        $questionListView->render();
                    }
                    else
                    {
                        if($optionID==null)
                        {
                            //*** xay dung view questioninput ***
                            //tham so truyen vao: $lectureID, $subjectID, $sectionID, $questionID
                            //tao view
                            $questionInputView = $this->loadView('questioninput');

                            $questionInputView->appendBlockData(array('lectureID'=>$lectureID));
                            $questionInputView->appendBlockData(array('subjectID'=>$subjectID));
                            $questionInputView->appendBlockData(array('sectionID'=>$sectionID));
                            $questionInputView->appendBlockData(array('questionID'=>$questionID));

                            if($questionID==-1)
                            {
                             //phan them cau hoi moi
                            }
                             else
                            {
                                //phan sua cau hoi
                                $aQuestion = $manageQuestionsModel->getQuestion($questionID);
                                $options = $manageQuestionsModel->getOptionList($questionID);
                                //truyen du lieu vao
                                $questionInputView->appendBlockData(array('options'=>$options));
                                foreach($aQuestion as $question)
                                    $questionInputView->appendBlockData(array('question'=>$question));

                            }
                            //ve
                            $questionInputView->addJs('nicEdit');
                            $questionInputView->render();


                        }
                        else
                        {
                            //*** xay dung optioninput ***
                            //tham so truyen vao: $lectureID, $subjectID, $sectionID, $questionID, $optionID
                            //tao view
                            $optionInputView = $this->loadView('optioninput');
                            $optionInputView->appendBlockData(array('lectureID'=>$lectureID));
                            $optionInputView->appendBlockData(array('subjectID'=>$subjectID));
                            $optionInputView->appendBlockData(array('sectionID'=>$sectionID));
                            $optionInputView->appendBlockData(array('questionID'=>$questionID));
                            $optionInputView->appendBlockData(array('optionID'=>$optionID));

                            if($optionID==-1)
                            {
                             //phan them dap an moi
                            }
                            else
                            {
                                //phan sua dap an
                                $aOption = $manageQuestionsModel->getOption($optionID);
                                foreach($aOption as $option)
                                    $optionInputView->appendBlockData(array('option'=>$option));
                            }

                            //ve
                            $optionInputView->addJs('nicEdit');
                            $optionInputView->render();
                        }

                    }
                }
            }
        }

    }

    public function saveNewQuestion()
    {
        $lectureID = $_POST["lectureID"];
        $subjectID = $_POST["subjectID"];
        $sectionID = $_POST["sectionID"];
        $contentQuestion = $_POST["contentQuestion"];
        $weightQuestion = $_POST["weightQuestion"];

        $manageQuestionsModel = $this->loadModel('managequestions');
        $newQuestionID = $manageQuestionsModel->saveNewQuestion($sectionID, $contentQuestion, $weightQuestion);
        if ($newQuestionID == -1)
            echo '<script type="text/javascript"> alert("Không thêm được!"); </script>';
        else
        {
            $this->index($subjectID, $sectionID, $newQuestionID);//quay ve trang sua cua chinh no.
            echo '<script type="text/javascript"> alert("Đã thêm thành công!"); </script>';
        }
    }

    public function updateQuestion()
    {
        $lectureID = $_POST["lectureID"];
        $subjectID = $_POST["subjectID"];
        $sectionID = $_POST["sectionID"];
        $questionID = $_POST["questionID"];
        $contentQuestion = $_POST["contentQuestion"];
        $weightQuestion = $_POST["weightQuestion"];

        $manageQuestionsModel = $this->loadModel('managequestions');
        $rs = $manageQuestionsModel->updateQuestion($questionID, $sectionID, $contentQuestion, $weightQuestion);
        if($rs)
        {
            $this->index($subjectID, $sectionID, $questionID);
            echo '<script type="text/javascript"> alert("Đã cập nhật thành công!"); </script>';
        }
        else
            echo '<script type="text/javascript"> alert("Không cập nhật được!"); </script>';
    }

    public function saveNewOption()
    {
        $lectureID = $_POST["lectureID"];
        $subjectID = $_POST["subjectID"];
        $sectionID = $_POST["sectionID"];
        $questionID = $_POST["questionID"];
        $contentOption = $_POST["contentOption"];
        $trueOrFalse = $_POST["trueOrFalse"];
        
        $manageQuestionsModel = $this->loadModel('managequestions');
        $newOptionID = $manageQuestionsModel->saveNewOption($questionID, $contentOption, $trueOrFalse);
        if ($newOptionID == -1)
            echo '<script type="text/javascript"> alert("Không thêm được!"); </script>';
        else
        {
            $this->index($subjectID, $sectionID, $questionID);
            echo '<script type="text/javascript"> alert("Đã thêm thành công!"); </script>';
        }


    }

    public function updateOption()
    {
        $lectureID = $_POST["lectureID"];
        $subjectID = $_POST["subjectID"];
        $sectionID = $_POST["sectionID"];
        $questionID = $_POST["questionID"];
        $optionID = $_POST["optionID"];
        $contentOption = $_POST["contentOption"];
        $trueOrFalse = $_POST["trueOrFalse"];

        $manageQuestionsModel = $this->loadModel('managequestions');
        $rs = $manageQuestionsModel->updateOption($optionID, $questionID, $contentOption, $trueOrFalse);
        if($rs)
        {
            $this->index($subjectID, $sectionID, $questionID);
            echo '<script type="text/javascript"> alert("Đã cập nhật thành công!"); </script>';
        }
        else
            echo '<script type="text/javascript"> alert("Không cập nhật được!"); </script>';
    }

    public function deleteQuestion($lectureID, $subjectID, $sectionID, $questionID)
    {
        $manageQuestionsModel = $this->loadModel('managequestions');
        //xoa option cua cau hoi truoc
        $rsOption = $manageQuestionsModel->deleteOptionList($questionID);
        //xoa cau hoi
        $rsQuestion = NULL;
        if($rsOption)
        {
            $rsQuestion = $manageQuestionsModel->deleteQuestion($questionID);
        }
        if($rsQuestion)
        {
            $this->index ($subjectID, $sectionID);
            echo '<script type="text/javascript"> alert("Đã xóa thành công!"); </script>';
        }
        else
            echo '<script type="text/javascript"> alert("Không xóa được!"); </script>';

    }

    public function deleteOption($lectureID, $subjectID, $sectionID, $questionID, $optionID)
    {
        $manageQuestionModel = $this->loadModel('managequestions');
        $rs = $manageQuestionModel->deleteOption($optionID);
        if($rs)
        {
            $this->index ($subjectID, $sectionID, $questionID);
            echo '<script type="text/javascript"> alert("Đã xóa thành công!"); </script>';
        }
            
        else
            echo '<script type="text/javascript"> alert("Không xóa được!"); </script>';
    }


}
?>